﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * CNR Error Class
 *      Provides a basic definition for repsenting a protocol error.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Text;
using Adaptive.Foundation;

namespace Adaptive.Foundation.Network.Cnr
{
    /// <summary>
    /// Provides a basic definition for repsenting a protocol error.
    /// </summary>
    public class CnrError<T> : CnrErrorBase 
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Error indicator / code value.
        /// </summary>
        private CnrErrorCode<T> _errorCode;
        /// <summary>
        /// Error data content.
        /// </summary>
        private string _errorData;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="errorCodeValue">
        /// The value that indicates the error.
        /// </param>
        public CnrError(T errorCodeValue)
            : base()
        {
            _errorCode = new CnrErrorCode<T>(errorCodeValue);
            _errorData = string.Empty;
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="errorCodeValue">
        /// The value that indicates the error.
        /// </param>
        /// <param name="description">
        /// A description of the error.
        /// </param>
        public CnrError(T errorCodeValue, string description)
            : base()
        {
            _errorCode = new CnrErrorCode<T>(errorCodeValue, description);
            _errorData = string.Empty;
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                if (_errorCode != null)
                    _errorCode.Dispose();
            }
            _errorCode = null;
            _errorData = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets the reference to the error code information.
        /// </summary>
        /// <value>
        /// A <see cref="T:CnrErrorCode"/> instance.
        /// </value>
        public CnrErrorCode<T> ErrorCode
        {
            get{return _errorCode;}
        }
        /// <summary>
        /// Gets or sets any associated text data for the error.
        /// </summary>
        /// <value>
        /// Text information associated with the error, or an empty string.
        /// </value>
        public string ErrorData
        {
            get{return _errorData;}
            set{_errorData = value;}
        }
        /// <summary>
        /// Gets or sets the name assigned to the object instance.
        /// </summary>
        /// <value>
        /// The name assigned to the <see cref="ErrorCode"/> object.
        /// </value>
        public override string Name
        {
            get
            {
                return _errorCode.Name;
            }
            set
            {
                _errorCode.Name = value;
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public override void Clear()
        {
            _errorData = string.Empty;
            _errorCode.Clear();
            base.Clear();
        }
        /// <summary>
        /// Gets a string representation of the object.
        /// </summary>
        /// <returns>
        /// A string representing the error code and, optionally, the error data text.
        /// </returns>
        public override string ToString()
        {
            StringBuilder builder = null;

            builder = new StringBuilder();
            builder.Append(_errorCode.ToString());

            if (_errorData.Length > 0)
            {
                builder.Append(" ");
                builder.Append(_errorData);
            }

            return builder.ToString();
        }
        #endregion
    }
}